package com.backend.help.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.github.xiaoymin.knife4j.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 描述：
 * 密码加密服务类
 * 加密服务 只提供加密不提供解密，避免密码泄露
 * @author xianggj
 * @Date 2021/8/26 16:05
 **/
@RestController
@RequestMapping("password")
@ApiSort(1)
@Api(value = "加密服务", tags="加密服务")
public class DbPassEncController {


    @Autowired
    private StringEncryptor stringEncryptor;

    @GetMapping("/enc")
    @ApiOperation(value = "明文转密文",notes = "数据库密码之类的可以通过这个借口转换", httpMethod = "GET")
    @ApiParam(name = "passwd",value="明文字符",required = true)
    public String encPassword(@RequestParam(name = "passwd",required = true) String passwd) {
        if (StrUtil.isBlank(passwd)) {
            return "加密密码不能为空";
        }
        return  stringEncryptor.encrypt(passwd);
    }

}
